// pages/addrecaddr/addrecaddr.js
let app = getApp();
let testAddr = app.globalData.testAddr;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    guestID:"",
    raId:"",
    description:"",
    provinceID:"",
    cityID:"",
    areaID:"",
    receiver_Person:"",
    receiver_Phone:"",
    update:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let raId=options.raId;
    let guestID = options.guestID;
    console.log(options);
    if(!raId){
      this.setData({
        guestID: guestID,
      });
    }else{
      this.setData({
        guestID: guestID,
        raId: raId,
        update:1,
      });
      this.queryDetailAddr();
    }


  },
  queryDetailAddr(){
    let that=this;
    app.util.request({
      url: testAddr + "receiver_address/searchInfo?guestID=" + that.data.guestID + "&raID=" +that.data.raId,
      method: "POST",
      success: function (res) {
        console.log(JSON.stringify(res.data));
        that.setData({
          guestID: res.data.guestID,
          raId: res.data.raID ? res.data.raID : "",
          description: res.data.description ? res.data.description : "",
          provinceID: res.data.provinceID ? res.data.provinceID : "",
          cityID: res.data.cityID ? res.data.cityID : "",
          areaID: res.data.areaID ? res.data.areaID : "",
          receiver_Person: res.data.receiver_Person ? res.data.receiver_Person : "",
          receiver_Phone: res.data.receiver_Phone ? res.data.receiver_Phone:""
        });
        
      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  addrPerson(e){
    this.setData({
      receiver_Person:e.detail.value
    });
  },
  addrPhone(e){
    this.setData({
      receiver_Phone: e.detail.value
    });
  },
  addrDetail(e){
    this.setData({
      description: e.detail.value
    });
  },
  addAddr(){
    if(this.data.update==1){
      let that = this;
      console.log(this.data.receiver_Person + "|" + this.data.receiver_Phone + "|" + this.data.description);
      wx.showModal({
        title: '修改地址',
        content: '确认修改此地址吗',
        success: function (res) {
          if (res.confirm) {
            app.util.request({
              url: testAddr + "receiver_address/edit?guestID=" + that.data.guestID + "&description=" + that.data.description + "&provinceID=" + that.data.provinceID + "&cityID=" + that.data.cityID + "&areaID=" + that.data.areaID + "&receiver_Person=" + that.data.receiver_Person + "&receiver_Phone=" + that.data.receiver_Phone + "&raID="+that.data.raId,
              method: "POST",
              success: function (res) {
                console.log(JSON.stringify(res.data));
                wx.showToast({
                  title: '修改成功',
                  icon: "none"
                });
                wx.navigateBack({ delta: 1 })
              },
              fail: function (res) {

              },
              complete: function (res) {

              },
            });
          } else {

          }
        }
      })
    }else{
      let that = this;
      console.log(this.data.receiver_Person + "|" + this.data.receiver_Phone + "|" + this.data.description);
      wx.showModal({
        title: '添加地址',
        content: '确认添加此地址吗',
        success: function (res) {
          if (res.confirm) {
            app.util.request({
              url: testAddr + "receiver_address/insert?guestID=" + that.data.guestID + "&description=" + that.data.description + "&provinceID=" + that.data.provinceID + "&cityID=" + that.data.cityID + "&areaID=" + that.data.areaID + "&receiver_Person=" + that.data.receiver_Person + "&receiver_Phone=" + that.data.receiver_Phone,
              method: "POST",
              success: function (res) {
                console.log(JSON.stringify(res.data));
                wx.showToast({
                  title: '添加成功',
                  icon: "none"
                });
                wx.navigateBack({ delta: 1 })
              },
              fail: function (res) {

              },
              complete: function (res) {

              },
            });
          } else {

          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})